package com.sharemarking.application.common.service;

import java.io.File;
import java.security.NoSuchAlgorithmException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Random;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.sharemarking.application.common.entity.Employee;
import com.sharemarking.application.common.entity.Notice;
import com.sharemarking.application.common.entity.Share;
import com.sharemarking.application.common.repository.EmployeeRepo;
import com.sharemarking.application.common.repository.NoticeRepo;
import com.sharemarking.application.common.repository.ShareRepo;
import com.sharemarking.application.common.util.Util;

@Controller
@RequestMapping("share1")
public class ShareController1 {

	@Autowired
	ShareRepo sr;

	@Autowired
	EmployeeRepo er;
	
	@Autowired
	NoticeRepo nr;

	/**
	 * 分享资源数据显示
	 * 
	 * @param s
	 * @return
	 */
	@RequestMapping("index")
	@ResponseBody
	public List<Share> getAllShare(Share s,HttpServletResponse res) {
		/**设置响应头允许ajax跨域访问**/
		res.setHeader("Access-Control-Allow-Origin","*");
		List<Share> list = null;
		Sort order = new Sort(Direction.DESC, "id");
		list = (List<Share>) sr.findAll(order);

		return list;
	}

	// 分享图文信息
	@PostMapping("shareSource")
	@ResponseBody
	public void shareSource(String loginName, String describe1, HttpServletRequest request,
			String Data) throws NoSuchAlgorithmException {
		System.out.println("describe1:" + describe1);
		System.out.println("sendImgData:" + Data);
		System.out.println("loginName:"+loginName);
		
		
		
//		String result = "";
		// 获取classes下的路径
		String path = this.getClass().getClassLoader().getResource("").getPath() + "public/uploadImage/share/";
		String path4 = this.getClass().getClassLoader().getResource("").getPath() + "public/uploadImage/share";
		File file = new File(path4);
		file.mkdirs();

		// 图片命名为loginName+Data+8位随机数.png
		Date now = new Date();
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmmss");// 可以方便地修改日期格式

		String Date = dateFormat.format(now);
		StringBuilder str = new StringBuilder();// 定义变长字符串

		// 随机生成数字，并添加到字符串
		Random random = new Random();
		for (int i = 0; i < 8; i++) {
			str.append(random.nextInt(10));
		}

		String path2 = path + Date + str.toString() + ".png";
		String path3 = Date + str.toString() + ".png";

			Employee e = er.findByLoginName(loginName);
			if (e != null) {
				boolean img = Util.GenerateImage(Data, path2);// 图片解析
				if(img){
					
				
				Share share = new Share();
				share.setImgUrl(path3);
				share.setPhoto(path3);
				share.setDescribe1(describe1);
				share.setEmployee(e);
				sr.save(share);
			}
				if (e.getJob().equals("经理")) {
					//System.out.println("你是一个好经理!");
				}

			}

	}

	// 删除图文信息
	@GetMapping(value = "deleteShare")
	@ResponseBody
	public String detete(Integer id) {
		System.out.println("删除id:" + id);
		sr.delete(id);
		return "success";
	}
	
	
	//公告数据展示
	@RequestMapping("index2")
	@ResponseBody
	public List<Notice> getAllNotice(HttpServletResponse res){
		/**设置响应头允许ajax跨域访问**/
		res.setHeader("Access-Control-Allow-Origin","*");
		List<Notice> list=null;
		Sort sort=new Sort(Direction.DESC,"id");
		list=(List<Notice>) nr.findAll(sort);
		
		return list;
	}
	
	//根据时间找公告
			@RequestMapping("ANotice")
			@ResponseBody
			public Notice getANotice(Date creatDate,HttpServletResponse res) {
				/**设置响应头允许ajax跨域访问**/
				res.setHeader("Access-Control-Allow-Origin","*");
				Notice n=new Notice();
				try{
					Sort sort=new Sort(Direction.DESC,"id");
					
					List<Notice> list=null;
					list=(List<Notice>) nr.findAll(sort);
					if(list.size()!=0){
						n=list.get(0);
						return n;
					}
					
					
					}catch(Exception e){
					e.printStackTrace();
					}
				
				return n;
				
			}
		
			//验证是否是员工
			@RequestMapping("checkWho")
			@ResponseBody
			public String checkWho(String loginName,HttpServletResponse res){
				/**设置响应头允许ajax跨域访问**/
				res.setHeader("Access-Control-Allow-Origin","*");

				Employee e=er.findByLoginName(loginName);
				String result="";
				if(e!=null){
					if(e.getCompanyId()==null){
						
						
						result="false";
						return result;
					}else{
						
						result="true";
						return result;
					}
				}
			
				return result;
				
				
			}
	
	//验证是管理员才能发布公告
	@RequestMapping("checkNotice")
	@ResponseBody
	public String checkNotice(String loginName,HttpServletResponse res){
		/**设置响应头允许ajax跨域访问**/
		res.setHeader("Access-Control-Allow-Origin","*");
		Employee e=er.findByLoginName(loginName);
		String result="";
		if(e!=null){
			if(e.getJob().equals("部门经理")||e.getJob()=="部门经理"||e.getJob().equals("企业老板")||e.getJob()=="企业老板"){
				System.out.println("你可以发公告");
				result="true";
				return result;
			}else if(e.getJob().equals("")||e.getJob()==""){
				System.out.println("你不可以发公告");
				result="false";
				return result;
			}else{
				System.out.println("你不可以发公告");
				result="false";
				return result;
			}
		}
		return null;
		
		
	}
	
	
	//发布公告
	@PostMapping("notice1")
	@ResponseBody
	public String publicNotice(String loginName,String title,String content,HttpServletResponse res){
		/**设置响应头允许ajax跨域访问**/
		res.setHeader("Access-Control-Allow-Origin","*");
		System.out.println("title:"+title);
		System.out.println("content:"+content);
		String result="";
		Notice notice=new Notice();
		Employee e=er.findByLoginName(loginName);
		
		if(e!=null){
				notice.setEmployee(e);
				notice.setTitle(title);
				notice.setContent(content);
				nr.save(notice);
				result="true";
				return result;
		}else{
				result="false";
			}
		return result;
	}
		
	
	//删除公告
	@GetMapping("deleteNotice")
	@ResponseBody
	public String deleteNotice(Integer id){
		nr.delete(id);
		return "删除成功";
	}
	
}
